package com.cqggrw.action;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

import com.cqggrw.domain.Json;
import com.cqggrw.domain.News;
import com.cqggrw.service.NewsServiceI;
import com.cqggrw.service.impl.NewsServiceImpl;
import com.cqggrw.util.Utils;
import com.opensymphony.xwork2.ModelDriven;

public class NewsAction extends BaseAction implements ModelDriven<News> {

	private static final long serialVersionUID = 1L;
	private NewsServiceI<News> service = new NewsServiceImpl();
	private String falg = null;
	private News news = new News();
	String separator = java.io.File.separator;

	public void aa() {
		String url = ServletActionContext.getRequest().getRequestURL().toString();
		String realPath = ServletActionContext.getServletContext().getRealPath(
				"");
		System.out.println(url);
		System.out.println(realPath);
	}

	public String add() {
		String realPath = ServletActionContext.getServletContext().getRealPath(
				separator + "images" + separator + "img");
		if (service.add(news, realPath)) {

		}
		return "success";
	}

	public void getCate() {
		super.writeJson(service.getCate());
	}

	public void getImgByFalg() {
		super.writeJson(service.getImgByFalg(news));
	}

	public void delete() {
		Json json = new Json();
		if (service.del(news.getId())) {
			json.setSuccess(true);
			json.setMsg("删除成功!");
		} else {
			json.setSuccess(false);
			json.setMsg("删除失败!");
		}

		super.writeJson(json);
	}

	public void addNewsImg() {
		String realPath = ServletActionContext.getServletContext().getRealPath(
				separator + "images" + separator + "img");
		Json json = new Json();
		if (service.addNewsImg(news, realPath)) {
			json.setSuccess(true);
			json.setMsg("添加成功!");
		} else {
			json.setSuccess(false);
			json.setMsg("添加失败!");
		}

		super.writeJson(json);
	}
	
	public void updateNewsImg() {
		String realPath = ServletActionContext.getServletContext().getRealPath(
				separator + "images" + separator + "img");
		Json json = new Json();
		if (service.updateNewsImg(news, realPath)) {
			json.setSuccess(true);
			json.setMsg("修改成功!");
		} else {
			json.setSuccess(false);
			json.setMsg("修改失败!");
		}

		super.writeJson(json);
	}
	
	public void delNewsImg() {
		Json json = new Json();
		if (service.delNewsImg(news)) {
			json.setSuccess(true);
			json.setMsg("修改成功!");
		} else {
			json.setSuccess(false);
			json.setMsg("修改失败!");
		}

		super.writeJson(json);
	}

	public String modify() {
		service.modify(news);
		return "modifySuccess";
	}

	public void getOne() {
		super.writeJson(service.get(news));
	}

	public void get() {
		super.writeJson(service.get());
	}

	public void getList() {
		super.writeJson(service.get(news, 0));
	}

	public News getModel() {
		return news;
	}

	public void picUpload() throws IOException {
		String fileName = UUID.randomUUID().toString() + "."
				+ news.getFileName().replace(".", ",").split(",")[1];
		String url = Utils.separator + "images" + Utils.separator + "img"
				+ Utils.separator + fileName;
		Map<String, String> map = new HashMap<String, String>();
		map.put("url", fileName);
		map.put("state", "图片上传失败！");
		map.put("title", fileName);
		String realPath = ServletActionContext.getServletContext().getRealPath(
				Utils.separator + "images" + Utils.separator + "img");
		if (news.getUpfile() != null) {
			File saveFile = new File(new File(realPath), fileName);
			if (!saveFile.getParentFile().exists()) {
				saveFile.getParentFile().mkdirs();
			}
			FileUtils.copyFile(news.getUpfile(), saveFile);
			map.put("state", "SUCCESS");
		}
		super.writeJson(map);
	}

	public News getNews() {
		return news;
	}

	public void setNews(News news) {
		this.news = news;
	}

	public String getFalg() {
		return falg;
	}

	public void setFalg(String falg) {
		this.falg = falg;
	}

}
